package com.qf.data.view.facade.service.device;

import com.qf.data.core.dal.po.device.DevicePO;
import com.qf.data.view.core.service.device.DeviceService;
import com.qf.data.view.facade.api.DeviceFacade;
import com.qf.data.view.facade.request.device.DeviceModelRequest;
import com.qf.data.view.facade.response.device.DeviceModelResponse;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Objects;

/**
 * @author q
 * @version V1.0
 * @date 2022/01/04
 **/
@Service
public class DeviceFacadeImpl implements DeviceFacade {

    @Autowired
    private DeviceService deviceService;

    /**
     * 根据设备key查询设备信息
     * @param deviceModelRequest
     * @return
     */
    @Override
    public DeviceModelResponse getDeviceByKey(DeviceModelRequest deviceModelRequest) {
        if (StringUtils.isBlank(deviceModelRequest.getDeviceKey())){
            return null;
        }
        String deviceKey = deviceModelRequest.getDeviceKey();
        // 查询设备信息
        DevicePO devicePO = deviceService.selectByDeviceKey(deviceKey);
        // 创建结果对象
        DeviceModelResponse deviceModelResponse = new DeviceModelResponse();

        if (Objects.nonNull(devicePO)){
            // 复制Bean对象，把devicePO里的属性都复制到deviceModelResponse中
            //    前提：两个对象中属性的名称一样，且都配有getter，setter方法
            BeanUtils.copyProperties(devicePO, deviceModelResponse);
        }

        return deviceModelResponse;
    }

    @Override
    public Integer addDevice(DeviceModelRequest deviceModelRequest) {
        DevicePO devicePO = new DevicePO();
        BeanUtils.copyProperties(deviceModelRequest, devicePO);
        return deviceService.insert(devicePO);
    }

    @Override
    public Integer updateByDeviceId(DeviceModelRequest deviceModelRequest) {
        DevicePO devicePO = new DevicePO();
        BeanUtils.copyProperties(deviceModelRequest, devicePO);
        return deviceService.updateByPrimaryKey(devicePO);
    }
}
